#include "view_heart_rate.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_common.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "times/view_time_0.h"
#include "sports_mode.h"
#include "app_trace.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)

void view_heart_rate_animation(view_heart_rate_param const *param)
{
    if (param == 0 || param->file_index_0 < DISP_LFS_FILE_INDEX_BEGIN || param->file_index_0 >= DISP_LFS_FILE_INDEX_END || param->file_index_1 < DISP_LFS_FILE_INDEX_BEGIN ||
        param->file_index_1 >= DISP_LFS_FILE_INDEX_END)
    {
        return;
    }
    // float mean = get_photoplethysmography_threshold();
    // char mean_buff[64] = {0};
    // sprintf(mean_buff, "%f", mean);
    // int mean_x = 0;
    // int mean_y = 160;
    if (!param->is_wear)
    {
        _disp_show_flash_img(param->file_index_0, 25, 73, FILE_IMG_J115_notwearingawatch_25_73);
        _disp_show_flash_img(param->file_index_1, 18, 183, FILE_IMG_J115_notwearingawatch_text_18_183);
        // disp_show_font_string_utf8(mean_x, mean_y, mean_buff, 0);
        return;
    }
    int x = 70;
    int y = 38;
    _disp_show_flash_img(param->file_index_0, 47, 24, FILE_IMG_1004_heartrate_47_24);
    // img_gif_com_t const img_cfg = {
    //     .gif_count = 2,
    //     // .img[0].x = 49,
    //     // .img[0].y = 17,
    //     // .img[0].sn = FILE_IMG_A101_heartrate_1_49_17,
    //     .img[0].x = 49,
    //     .img[0].y = 17,
    //     .img[0].sn = FILE_IMG_A102_heartrate_2_49_17,
    //     .img[1].x = 49,
    //     .img[1].y = 17,
    //     .img[1].sn = FILE_IMG_A103_heartrate_3_49_17,
    // };
    // _disp_show_flash_img_gif_all(&img_cfg, param->file_index_0);
    // disp_show_font_string_utf8(mean_x, mean_y, mean_buff, 0);
    int unit_width = 0;
    if (param->unit_width > 0)
    {
        unit_width = param->unit_width;
    }
    int x_center = 120 - unit_width / 2;
    int const font_width = 16;
    int const num_width = font_width + 4;
    int const num_height = 37;
    int unit_height = num_height;
    if (param->unit_height > 0)
    {
        unit_height = param->unit_height;
    }
    y = 187;
    if (param->num < 0)
    {
        x = x_center - num_width;
        if (disp_ctrl.auto_refresh_count % 2 == 0)
        {
            int x1 = x;
            int y1 = y + num_height - unit_height / 2;
            int x2 = x1 + font_width;
            int y2 = y1 + 1;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);

            x1 += num_width;
            x2 = x1 + font_width;
            disp_draw_rect_color(x1, y1, x2, y2, RGB565_COLOR_WHITE);
        }
        else
        {
        }
        x += num_width * 2;
    }
    else
    {
        if (param->num_digit > 0)
        {
            x = disp_draw_user_image_num_mid_ignore_with_digit(x_center, y, RGB565_COLOR_BLACK, param->num, num_width, param->num_digit, img_j501_TAB);
        }
        else
        {
            x = disp_draw_user_image_num_mid_ignore(x_center, y, RGB565_COLOR_BLACK, param->num, num_width, img_j501_TAB);
        }
    }
    y += num_height - param->unit_height;
    disp_draw_user_image(x, y, param->unit);
}
void view_heart_rate_display(int view)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.i_temp[0] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
    if (disp_ctrl.com_data.i_temp[5])
    {
        disp_draw_user_image(42, 3, img_ble_connect);
    }
    else
    {
        disp_draw_user_image(42, 0, img_ble_disconnect);
    }
    show_common_time(disp_ctrl.com_data.i_temp[3], disp_ctrl.com_data.i_temp[4]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9], disp_ctrl.com_data.i_temp[8]);
    int num = disp_ctrl.com_data.i_temp[0];
    if (num == 0)
    {
        num = -1;
    }
    view_heart_rate_param const param = {
        .is_wear = disp_ctrl.com_data.i_temp[1],
        .num = num,
        .num_digit = 0,
        .unit = img_common_bpm,
        .file_index_0 = DISP_LFS_FILE_INDEX_0,
        .file_index_1 = DISP_LFS_FILE_INDEX_1,
        .unit_height = 19,
        .unit_width = 30,
    };
    view_heart_rate_animation(&param);
    // int const unit_width = 39;
}
view_event_result_t view_heart_rate_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    case event_view_entry:
        disp_ctrl.auto_close_delay = 60000;
        hr_test_start(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    case event_view_re_screen_on:
        disp_ctrl.auto_close_delay = 60000;
        return view_event_result_handled;
    case event_view_exit:
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
